package com.scenic.rd_android.threerd.rxjava_retrofit;

import com.scenic.rd_android.BaseTestClass;

import org.junit.Test;

import java.util.List;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Path;
import rx.Observable;
import rx.Subscriber;
import rx.functions.Action1;
import rx.internal.schedulers.CachedThreadScheduler;
import rx.internal.util.RxThreadFactory;
import rx.schedulers.Schedulers;

import static com.scenic.rd_android.threerd.rxjava_retrofit.TestJavaBeans.GitHubUserEntity;

/**
 * Created by scenic on 2016/8/25.
 */
public class RetrofitWithRxJava extends BaseTestClass {

    static class HttpResultSubscriber<T> extends Subscriber<T> {

        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {

        }

        @Override
        public void onNext(T t) {

        }
    }

    public interface GitHubService {
        @ServiceFactory.BaseUrl
        String baseUrl = "https://api.github.com";

        //        @GET("/users/{user}/repos")
        @GET("users/mojombo")
        Observable<GitHubUserEntity> oneBean();


        @GET("users/{user}")
        Observable<TestJavaBeans.GitHubUserEntity> getUser(@Path("user") String user);

        @GET("users")
        Observable<List<GitHubUserEntity>> getUsers();

        @GET("users")
        Observable<List<GitHubUserEntity>> getUsers_();
    }

    @Test
    public void testBegin() throws InterruptedException {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.github.com")
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        GitHubService gitHubService = retrofit.create(GitHubService.class);
        gitHubService.getUser("scenic2010")
                .observeOn(new CachedThreadScheduler(new RxThreadFactory("-----")))
                .subscribe(new Subscriber<GitHubUserEntity>() {
                    @Override
                    public void onCompleted() {
                        log("onCompleted");
                    }

                    @Override
                    public void onError(Throwable e) {
                        log("onError");
                    }

                    @Override
                    public void onNext(GitHubUserEntity javaBean) {
                        log("javaBean " + javaBean);
                    }
                })
//                .subscribe(new Action1<GitHubUserEntity>() {
//                    @Override
//                    public void call(GitHubUserEntity javaBean) {
//                        myLog.info("java bean is " + javaBean);
//                    }
//                })

        ;
        Thread.sleep(3000);

    }

    @Test
    public void testServiceFactory() {

        GitHubService value = ServiceFactory.create(GitHubService.class);

        Observable<List<GitHubUserEntity>> observable = value.getUsers();

        observable.subscribe(new HttpResultSubscriber<List<GitHubUserEntity>>() {
            @Override
            public void onNext(List<GitHubUserEntity> listHttpResult) {
                log(listHttpResult);
            }
        });
    }



    @Test
    public void testExtra() throws InterruptedException {

        final Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://121.40.213.101:10000")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();



        APITest api = retrofit.create(APITest.class);
        Observable<TestJavaBeans.GetServerInfo> obj = api.getServerInfo4("100000");//

        obj .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())

                .subscribe(new Action1<TestJavaBeans.GetServerInfo>() {
                    @Override
                    public void call(TestJavaBeans.GetServerInfo getServerInfo) {
                        System.out.println(getServerInfo);
                    }
                })
        ;

        Thread.sleep(2000);

    }


}
